package com.youkeda.app.control;

import com.youkeda.app.model.PersonalRecord;
import com.youkeda.app.model.Result;
import com.youkeda.app.service.PersonalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName PetsonalRecordController.java
 * @Description TODO
 * @createTime 2020年10月03日 15:24:00
 */
@Controller
public class PersonalRecordController {
    @Autowired
    private PersonalRecordService personalRecordService;
    @RequestMapping("/personalRecoed/query")
    @ResponseBody
    public Result<List<PersonalRecord>> queryLimitRank(Long limit){
        System.out.println("controll:"+limit);
        Result<List<PersonalRecord>> result = new Result<>();
        result.setSuccess(true);
        List<PersonalRecord> personalRecords = personalRecordService.queryLimit(limit);
        if (CollectionUtils.isEmpty(personalRecords)||personalRecords ==null){
            result.setSuccess(false);
            return result;
        }
        result.setData(personalRecords);
        return result;
    }
    @RequestMapping("/findByUserId")
    @ResponseBody
    public Result<PersonalRecord> findByUserId(Long userId){
        Result<PersonalRecord> result = new Result<>();
        result.setSuccess(true);
        PersonalRecord personalRecords = personalRecordService.findByUserId(userId);
        if (personalRecords ==null){
            result.setSuccess(false);
            return result;
        }
        result.setData(personalRecords);
        return result;
    }
    @GetMapping("/getRankIndex")
    @ResponseBody
    public Result<Long> getRankIndex(Long userId){
        Result<Long> result = new Result<>();
        result.setSuccess(true);
        Long rowNum = personalRecordService.getRankIndex(userId);
        if (rowNum == null){
            result.setSuccess(false);
            result.setMessage("无排名！");
            return result;
        }
        result.setData(rowNum);
        return result;
    }
}
